/**
 * Created by 小敏哥 on 2017/11/30.
 */
import React, {Component} from 'react';
import {connect} from 'react-redux';
import {getDefaultAddress} from "../../../actions/addAddressListActions";
import ItemWithRightArrow from '../../../../../../js/components/itemWithRightArrow';
import style from './index.scss';

class AddressPicker extends Component {
    constructor(props) {
        super(props);
    }

    componentDidMount(){
        if(!this.props.currentAddress){
         this.props.dispatch(getDefaultAddress({}));
        }
    }

    structContent(){
        if(this.props.currentAddress){
            return <div onClick={()=>{ this.context.router.push('addressList')}}>
                <ItemWithRightArrow>
                    <div className={style.simpleMessage}>
                        <img src="./images/map.png"/>
                        <div>{this.props.currentAddress.contactName+'  '+this.props.currentAddress.cellNum}</div>
                    </div>
                </ItemWithRightArrow>
                <p className={style.fullAddress}>{this.props.currentAddress.fullAddr}</p>
            </div>
        }
        else{
            return <ItemWithRightArrow onClick={()=>{ this.context.router.push('addressList')}}>
                <div className={style.simpleMessage}>
                    <img src="./images/map.png"/>
                    <div>添加收件人信息</div>
                </div>
            </ItemWithRightArrow>
        }
    }

    render(){
        return <div className={style.addressPicker}>
            {this.structContent()}
        </div>
    }
}

//使用context
AddressPicker.contextTypes = {
    router: React.PropTypes.object.isRequired
};


function mapStateToProps(state) {
    return Object.assign({}, state.currentAddress);
}



export default connect(mapStateToProps)(AddressPicker);